package com.sinaapp.mrblog.game.Assasin.stage;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.sinaapp.mrblog.game.Assasin.Assasin;
import com.sinaapp.mrblog.game.Assasin.render.BaseRender;
import com.sinaapp.mrblog.game.Assasin.screen.ChooseScreen;
import com.sinaapp.mrblog.game.Assasin.util.AssasinConst;
import com.sinaapp.mrblog.game.Assasin.util.AssasinManager;
import com.sinaapp.mrblog.game.Assasin.util.Author;
import com.sinaapp.mrblog.game.Assasin.util.Director;

public class ChooseSectionStage extends Stage implements BaseRender{

	int[] chapter;
	Image[] sections;
	
	public ChooseSectionStage() {
		super();
		chapter = Author.chapters.get(ChooseScreen.currentChapter);
		sections = new Image[chapter.length];
		float x = 0,y = 0;
		for(int i = 0;i<sections.length;i++){
			if(Author.isLocked(chapter[i])){
				sections[i] = new Image(AssasinManager.sectionlockRegion);
			}else{
				sections[i] = new Image(AssasinManager.sectionUnlockRegion);
			}			
			
			if(i==0){
				x = AssasinConst.CHOOSE_SECTION_INTERVAL;
				y =AssasinConst.GAMESTAGE_HEIGHT - AssasinConst.CHOOSE_SECTION_INTERVAL-AssasinConst.CHOOSE_SECTION_HEIGHT;
			}else{
				if((i)%AssasinConst.CHOOSE_SECTION_HORIZON==0){
					y -= AssasinConst.CHOOSE_SECTION_INTERVAL+AssasinConst.CHOOSE_SECTION_HEIGHT;
					x = AssasinConst.CHOOSE_SECTION_INTERVAL;
				}else{
					x += AssasinConst.CHOOSE_SECTION_INTERVAL+AssasinConst.CHOOSE_SECTION_WIDTH;
				}
			}
			
			sections[i].setPosition(x,y);
			sections[i].setSize(AssasinConst.CHOOSE_SECTION_WIDTH, AssasinConst.CHOOSE_SECTION_HEIGHT);
			addActor(sections[i]);
		}
		
		setCamera(Assasin.CAMERA);
		getCamera().position.set(AssasinConst.GAMESTAGE_WIDTH/2, AssasinConst.GAMESTAGE_HEIGHT/2, 0);
	}

	

	@Override
	public void render(float delta) {
		update(delta);
		act(delta);
		getSpriteBatch().begin();
		getSpriteBatch().draw(AssasinManager.choiceBGregion, getCamera().position.x-30, getCamera().position.y-20,60,40);
		getSpriteBatch().end();
		draw();
	}



	private void update(float delta) {
		if(Gdx.input.justTouched()){
			Vector2 tvec = Director.changeToGame(Gdx.input.getX(0), Gdx.input.getY(0));
			float touch_X = tvec.x;
			float touch_Y = tvec.y;
			for(int i = 0; i<sections.length;i++){
				if(Director.isInActorRectangle(touch_X, touch_Y, sections[i])){
					ChooseScreen.currentSection = chapter[i];
					ChooseScreen.state = ChooseScreen.CHOOSE_STATE_GAME;
					ChooseScreen.refresh();
				}
			}
		}
	}
	
}
